package com.shop.controller;

import com.shop.common.SetMeta;
import com.shop.entity.Manager;
import com.shop.exception.RightException;
import com.shop.service.ManagerService;
import com.shop.service.RightsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
public class RightsController {

    @Autowired
    public RightsService rightsService;

    @Autowired
    ManagerService managerService;

    /**
     * 所有权限列表
     * @param type
     * @return
     */
    @PreAuthorize("hasAnyAuthority('0')")
    @GetMapping("rights/{type}")
    public SetMeta selectRights(@PathVariable String type) {
        SetMeta setMeta = new SetMeta();
        try {
            setMeta = rightsService.selectRights(type);
        } catch (RightException e) {
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 左侧菜单权限
     * @return
     */
    @PreAuthorize("hasAnyAuthority('0')")
    @GetMapping("menus")
    public SetMeta selectLeftRights(@RequestParam(required = false) String username) {
        Manager manager = managerService.getUsername(username);
        return rightsService.selectLeftRights(manager.getRid());
    }
}
